package com.customization.test.demo.job;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import weaver.conn.RecordSetTrans;
import weaver.general.Util;
import weaver.integration.logging.Logger;
import weaver.integration.logging.LoggerFactory;
import weaver.interfaces.schedule.BaseCronJob;

/**
 * @description 自定义定时任务
 * @create 2024-04-22 10:29
 */
public class SyncRequestTitleJob  extends BaseCronJob {

    private static final Logger log = LoggerFactory.getLogger(SyncRequestTitleJob.class);

    public void execute() {

        // 数据库操作
        RecordSetTrans rst = new RecordSetTrans();
        // 开启事务（关闭自动提交）
        rst.setAutoCommit(false);
        // String sql = "update hrmresource lastname=? where id=?";
        String sql = "select * from  HrmSubCompany";
        try {
            // int a = 1/0;
            // 新增或修改
            // rst.executeUpdate(sql, "猪八戒", 2);

            // 查询
            rst.executeQuery(sql);

            // 提交事务
            rst.commit();
            // 获取所有数据
            JSONArray datas = new JSONArray();
            String[] columnNames = rst.getColumnName();
            while(rst.next()){
                JSONObject jsonObj = new JSONObject();
                for(String columnName : columnNames){
                    jsonObj.put(columnName.toLowerCase(), Util.formatMultiLang(rst.getString(columnName)));
                }
                datas.add(jsonObj);
            }

            // 获取第一个数据
//            if(rst.first()) {
//                String id = Util.null2String(rst.getString("id"));//角色ID;
//                baseBean.writeLog(id);
//            }else {
//                baseBean.writeLog("无数据");
//            }
        } catch (Exception e) {
            log.info("定时任务异常："+e.toString());
            e.printStackTrace();
            // 事务回滚
            rst.rollback();
        }

    }
}

